//
//  ReHeatViewController.m
//  一点互助
//
//  Created by rimi on 16/8/24.
//  Copyright © 2016年 zhangboqiu. All rights reserved.
//

#import "ReHeatViewController.h"
#import "UIViewController+NavagitionBar.h"
#import "ResponseManager.h"
#import "WishesModel.h"
#import "ReheatTableViewCell.h"
#import "DetailViewController.h"

@interface ReHeatViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) NSMutableArray *dataSource;
@property (strong, nonatomic) UITableView *tableView;
@end

@implementation ReHeatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.tableView];
    UINib *nib = [UINib nibWithNibName:@"ReheatTableViewCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"ReheatTableViewCell"];
    [self setupBackItem];
    [self getRequest];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tabBarController.tabBar setHidden:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.tabBarController.tabBar setHidden:NO];
}
-(void)getRequest{
    __weak typeof(self) myself = self;
    NSDictionary *parameter = @{@"wish_type":@(2)};
    [ResponseManager startGetRequestWithURLString:kGetWishListURL parameters:parameter completionHandler:^(FBResponse *response) {
        if (response.success) {
            PLLOG(@"%@",response.resultValue);
            NSArray *arr = [WishesModel modelArrayWithDictionaryArr:response.resultValue];
            myself.dataSource = [arr mutableCopy];
            [myself.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
        }else{
            [self addAlertControllerWithTitle:@"提示" message:response.resultDesc actionNum:2 style:UIAlertControllerStyleAlert handler:^(UIAlertAction *action) {
                [myself getRequest];
            }];
        }
    }];

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ReheatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReheatTableViewCell" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    WishesModel *model = self.dataSource[indexPath.row];
    CGRect rect = [model.wishDesc boundingRectWithSize:CGSizeMake(60*kScreenWidthScale, 300 * kScreenHeightScale) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil];
    if (rect.size.height < 50) {
        PLLOG(@"小%f",rect.size.height);
        return 80;
    }else{
        PLLOG(@"大%f",rect.size.height);
        return rect.size.height * kScreenHeightScale;
    }
}
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailViewController *vc = [[DetailViewController alloc]init];
    vc.model = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
